//
//  LZVideoReviewListViewController.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/17.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZVideoReviewListViewController.h"
#import "LAMerchantDetailReviewTableView.h"
#import "LAWriteReviewViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "LZVideoPresenter.h"
#import "LZReviewCountModel.h"
#import "LZUserModel.h"
#import "LZLoginViewController.h"
#import "Masonry.h"

@interface LZVideoReviewListViewController ()<SubTableViewDelegate,RestaurantReviewTableViewDelegate>

@property (nonatomic, strong) LAMerchantDetailReviewTableView *table;
@property (strong, nonatomic) UIButton *rightButton;

@end

@implementation LZVideoReviewListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.umLogPageViewName = @"视频评论页面";
    
    [self configTitleAndRightButton];
    
    [self configTable];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - request method
// 优惠视频--评论数量统计
- (void)requestCommentCountSuccess:(void (^)(id))successBlock {
    WS(weakSelf)
    [LZVideoPresenter getVideoCommentNumWithVideoID:self.videoId ResultSuccess:^(id result) {
        LZReviewCountModel *model = (LZReviewCountModel*)result;
        if (weakSelf.refreshNumBlock) {
            weakSelf.refreshNumBlock(model.total);
        }
        if (successBlock) {
            successBlock(result);
        }
    } ResultFailed:^(NSString *remind) {
        if (successBlock) {
            successBlock(nil);
        }
    }];
}

// 优惠视频--评论列表
- (void)requestVideoReviewListWithPrameter:(NSDictionary*)prameter andPage:(int)page andLimit:(int)limit resultSuccess:(void (^)(NSArray *,NSNumber *))successBlock{
    
    // 评论类型（0全部，1好评，2差评，3有图；不填为全部）
    [LZVideoPresenter getVideoCommentListWithPrameter:prameter PromotionID:self.videoId CurrentPage:page PageLimit:limit ResultSuccess:^(NSArray *dataArr,NSNumber *timeStamp) {
        if (successBlock) {
            successBlock(dataArr,timeStamp);
        }
    } ResultFailed:^(NSString *remind) {
        if (successBlock) {
            successBlock(nil,nil);
        }
    }];
    
}

#pragma mark - 创建title和导航栏右边按钮
- (void)configTitleAndRightButton {
    self.title = LZLocalizedString(@"review_list", nil);
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightButton setTitle:LZLocalizedString(@"review_write", nil) forState:UIControlStateNormal];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.rightButton setTitleColor:UIColorFromRGB(0xff7a21, 1) forState:UIControlStateNormal];
    [self.rightButton sizeToFit];
    self.rightButton.frame = CGRectMake(0, 0, CGRectGetWidth(self.rightButton.bounds), 40);

    [self.rightButton addTarget:self action:@selector(navigationBarRightButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self navigationItemSetting:@[self.rightButton] type:NAVIGATIONITEMSETTING_RIGHT];
}

// 导航栏右边按钮点击事件
- (void)navigationBarRightButtonClickAction:(UIButton*)sender {
    if ([[LZUserModel Instance] isLogin]) {
        LAWriteReviewViewController *vc = [[LAWriteReviewViewController alloc] initWithNibName:@"LAWriteReviewViewController" bundle:nil] ;
        vc.type = WriteReviewType_Video;
        vc.sellerId = [self.restaurantId copy];
        vc.videoId = [self.videoId copy];
        [self.navigationController pushViewController:vc animated:YES];
        
        WS(weakSelf)
        vc.uploadReviewBlock = ^(BOOL isUploadSuccess) {
            if (isUploadSuccess) {
                [weakSelf.table upData];
            }
        };
    }
    else {
        // 如果未登录
        LZLoginViewController * vc = [[LZLoginViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - 创建table
- (void)configTable {
    self.table = [[LAMerchantDetailReviewTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.table.isHaveLinkage = NO;
    self.table.isShowNoDataImageView = YES;
    self.table.subTableDelegate = self;
    self.table.reviewTableDelegate = self;
    self.table.reviewType = ReviewType_Video;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.leading.and.trailing.and.bottom.equalTo(0);
    }];
    
    [self.table checkHaveData];
}

#pragma mark RestaurantReviewTableViewDelegate
- (void)requestCommentCountResultSuccess:(void (^)(id))successBlock {
    [self requestCommentCountSuccess:successBlock];
}

#pragma mark - SubTableViewDelegate

-(void)requestDateWithType:(TableViewType)tableType andPrameter:(NSDictionary *)prameter andPage:(int)page andLimit:(int)limit resultSuccess:(void (^)(NSArray *,NSNumber *))successBlock {
    if (tableType == TableViewType_Review) {
        [self requestVideoReviewListWithPrameter:prameter andPage:page andLimit:limit resultSuccess:successBlock];
    }
}

@end
